{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bert import modeling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /home/husein/.local/lib/python3.6/site-packages/bert/modeling.py:93: The name tf.gfile.GFile is deprecated. Please use tf.io.gfile.GFile instead.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "bert_config = modeling.BertConfig.from_json_file('BASE_config.json')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'vocab_size': 32128,\n",
       " 'hidden_size': 768,\n",
       " 'num_hidden_layers': 12,\n",
       " 'num_attention_heads': 12,\n",
       " 'hidden_act': 'gelu',\n",
       " 'intermediate_size': 3072,\n",
       " 'hidden_dropout_prob': 0.1,\n",
       " 'attention_probs_dropout_prob': 0.1,\n",
       " 'max_position_embeddings': 1024,\n",
       " 'type_vocab_size': 2,\n",
       " 'initializer_range': 0.02,\n",
       " 'directionality': 'bidi',\n",
       " 'pooler_fc_size': 768,\n",
       " 'pooler_num_attention_heads': 12,\n",
       " 'pooler_num_fc_layers': 3,\n",
       " 'pooler_size_per_head': 128,\n",
       " 'pooler_type': 'first_token_transform'}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bert_config.__dict__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'mesolitica-storage.json'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from google.cloud import storage\n",
    "client = storage.Client()\n",
    "bucket = client.bucket('mesolitica-general')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "!rm -rf base\n",
    "!mkdir base"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "files = [\n",
    "'model.ckpt-175000.data-00000-of-00001',\n",
    "'model.ckpt-175000.index',\n",
    "'model.ckpt-175000.meta']\n",
    "\n",
    "for file in files:\n",
    "    blob = bucket.blob(f'b2b-base/{file}')\n",
    "    blob = blob.download_to_filename(f'base/{file}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /home/husein/.local/lib/python3.6/site-packages/bert/optimization.py:87: The name tf.train.Optimizer is deprecated. Please use tf.compat.v1.train.Optimizer instead.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import bert\n",
    "from bert import run_classifier\n",
    "from bert import optimization\n",
    "from bert import tokenization\n",
    "from bert import modeling\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "import pandas as pd\n",
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "bert_config = modeling.BertConfig.from_json_file('BASE_config.json')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /home/husein/b2b/transformer/attention_layer.py:24: The name tf.layers.Layer is deprecated. Please use tf.compat.v1.layers.Layer instead.\n",
      "\n",
      "WARNING:tensorflow:From /home/husein/b2b/b2t.py:33: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.\n",
      "\n",
      "WARNING:tensorflow:From /home/husein/.local/lib/python3.6/site-packages/tensorflow_core/python/util/deprecation.py:507: calling count_nonzero (from tensorflow.python.ops.math_ops) with axis is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "reduction_indices is deprecated, use axis instead\n",
      "WARNING:tensorflow:From /home/husein/.local/lib/python3.6/site-packages/bert/modeling.py:171: The name tf.variable_scope is deprecated. Please use tf.compat.v1.variable_scope instead.\n",
      "\n",
      "WARNING:tensorflow:From /home/husein/.local/lib/python3.6/site-packages/bert/modeling.py:409: The name tf.get_variable is deprecated. Please use tf.compat.v1.get_variable instead.\n",
      "\n",
      "WARNING:tensorflow:From /home/husein/.local/lib/python3.6/site-packages/bert/modeling.py:490: The name tf.assert_less_equal is deprecated. Please use tf.compat.v1.assert_less_equal instead.\n",
      "\n",
      "WARNING:tensorflow:\n",
      "The TensorFlow contrib module will not be included in TensorFlow 2.0.\n",
      "For more information, please see:\n",
      "  * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md\n",
      "  * https://github.com/tensorflow/addons\n",
      "  * https://github.com/tensorflow/io (for I/O related ops)\n",
      "If you depend on functionality not listed there, please file an issue.\n",
      "\n",
      "WARNING:tensorflow:From /home/husein/.local/lib/python3.6/site-packages/bert/modeling.py:671: dense (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use keras.layers.Dense instead.\n",
      "WARNING:tensorflow:From /home/husein/.local/lib/python3.6/site-packages/tensorflow_core/python/layers/core.py:187: Layer.apply (from tensorflow.python.keras.engine.base_layer) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use `layer.__call__` method instead.\n",
      "WARNING:tensorflow:From /home/husein/b2b/transformer/attention_layer.py:41: The name tf.layers.Dense is deprecated. Please use tf.compat.v1.layers.Dense instead.\n",
      "\n",
      "WARNING:tensorflow:From /home/husein/b2b/transformer/model_utils.py:93: to_float (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use `tf.cast` instead.\n",
      "WARNING:tensorflow:From /home/husein/b2b/transformer/model_utils.py:75: The name tf.matrix_band_part is deprecated. Please use tf.linalg.band_part instead.\n",
      "\n",
      "WARNING:tensorflow:From /home/husein/.local/lib/python3.6/site-packages/tensorflow_core/python/autograph/converters/directives.py:119: The name tf.rsqrt is deprecated. Please use tf.math.rsqrt instead.\n",
      "\n",
      "Tensor(\"cls/predictions/MatMul:0\", shape=(?, ?, 32128), dtype=float32)\n",
      "WARNING:tensorflow:From /home/husein/.local/lib/python3.6/site-packages/tensorflow_core/python/ops/array_ops.py:1475: where (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use tf.where in 2.0, which has the same broadcast rule as np.where\n",
      "WARNING:tensorflow:From /home/husein/b2b/transformer/beam_search.py:489: calling reduce_logsumexp_v1 (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "keep_dims is deprecated, use keepdims instead\n"
     ]
    }
   ],
   "source": [
    "import b2t\n",
    "\n",
    "tf.reset_default_graph()\n",
    "sess = tf.InteractiveSession()\n",
    "model = b2t.Model(bert_config, is_training = False)\n",
    "\n",
    "sess.run(tf.global_variables_initializer())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Restoring parameters from base/model.ckpt-175000\n"
     ]
    }
   ],
   "source": [
    "saver = tf.train.Saver(var_list = tf.trainable_variables())\n",
    "saver.restore(sess, 'base/model.ckpt-175000')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "from prepro_utils import preprocess_text, encode_ids, encode_pieces\n",
    "import sentencepiece as spm\n",
    "\n",
    "sp_model = spm.SentencePieceProcessor()\n",
    "sp_model.Load('sp10m.cased.t5.model')\n",
    "\n",
    "with open('sp10m.cased.t5.vocab') as fopen:\n",
    "    v = fopen.read().split('\\n')[:-1]\n",
    "v = [i.split('\\t') for i in v]\n",
    "v = {i[0]: i[1] for i in v}\n",
    "\n",
    "class Tokenizer:\n",
    "    def __init__(self, v):\n",
    "        self.vocab = v\n",
    "        pass\n",
    "    \n",
    "    def tokenize(self, string):\n",
    "        return encode_pieces(sp_model, string, return_unicode=False, sample=False)\n",
    "    \n",
    "    def convert_tokens_to_ids(self, tokens):\n",
    "        return [sp_model.PieceToId(piece) for piece in tokens]\n",
    "    \n",
    "    def convert_ids_to_tokens(self, ids):\n",
    "        return [sp_model.IdToPiece(i) for i in ids]\n",
    "    \n",
    "tokenizer = Tokenizer(v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "from unidecode import unidecode\n",
    "\n",
    "def get_inputs(x):\n",
    "    input_ids, input_masks, segment_ids = [], [], []\n",
    "    for i in tqdm(range(len(x))):\n",
    "        tokens_a = tokenizer.tokenize(unidecode(x[i]))\n",
    "        tokens = tokens_a\n",
    "        \n",
    "        segment_id = [0] * len(tokens)\n",
    "        input_id = tokenizer.convert_tokens_to_ids(tokens)\n",
    "        input_mask = [1] * len(input_id)\n",
    "\n",
    "        input_ids.append(input_id)\n",
    "        input_masks.append(input_mask)\n",
    "        segment_ids.append(segment_id)\n",
    "    \n",
    "    return input_ids, input_masks, segment_ids"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# https://harakahdaily.net/index.php/2020/05/22/apa-ph-buat-isu-kenaikan-harga-barang/\n",
    "\n",
    "string2 = \"\"\"\n",
    "SEKARANG PH tengah mainkan isu harga barang untuk memburukkan Kerajaan PN. Kata Ahli Majlis Setiausaha PH (Saifuddin Nasution, Khalid Samad dan Ong Kian Ming) harga barang mentah dan basah menunjukkan ada peningkatan di banyak tempat. Ini membebankan rakyat kata mereka.\n",
    "\n",
    "Saya setuju, Kerajaan PN perlu pantau dan ambil apa-apa tindakan yang perlu jika ada peningkatan harga barang basah atau mentah.\n",
    "\n",
    "Tapi dalam masa yang sama saya fikir elok juga kalau kita imbas balik apa pula tindakan yang diambil oleh Kerajaan PH ketika rakyat tertekan dengan harga barang yang melonjak gila-gila masa mereka jadi Kerajaan Persekutuan selama 22 bulan dulu.\n",
    "\n",
    "Mari kita semak balik.\n",
    "\n",
    "(1) Kenaikan harga telur\n",
    "Saifuddin Nasution – “Kementerian akan siasat.”\n",
    "Salahuddin Ayub – “Ianya adalah perkara biasa disebabkan kenaikan bahan makanan ayam.”\n",
    "\n",
    "(2) Kenaikan harga bawang 3 kali ganda\n",
    "Saifuddin Nasution – “KPNHEP akan pantau kenaikan ini. Rakyat perlu tukar kepada bawang Holland.”\n",
    "\n",
    "(3) Kenaikan harga ikan kembung\n",
    "Salahuddin Ayub – “Saya pelik mengapa harga ikan kembung naik sedangkan ada 7 bilion ikan kembung dalam laut.”\n",
    "\n",
    "(4) Kenaikan harga barangan kawalan\n",
    "Saifuddin Nasution – “KPNHEP tidak menerima sebarang aduan kenaikan harga barang kawalan. Kita akan siasat.”\n",
    "\n",
    "(5) Kenaikan harga barang mentah dan basah menjelang Aidilfitri 2019\n",
    "Saifuddin Nasution – “KPNHEP puas hati dengan trend semasa harga barang keperluan. Tiada kenaikan mendadak (kenaikan sedikit demi sedikit itu perkara biasa).”\n",
    "\n",
    "(6) Kenaikan harga barang keperluan pada penghujung 2018 dan sepanjang 2019\n",
    "Lim Guan Eng – “Kenaikan harga barang disebakan banyak faktor seperti penyusutan nilai Ringgit, musim perayaan, kegiatan penyeludupan, kemarau di negara pengeluar selain sikap peniaga yang mahu mengaut keuntungan berlebihan. Ianya tidak dapat dielakkan.”\n",
    "\n",
    "Yang saya listkan di atas hanya sebahagian kecil daripada kenyataan Menteri-menteri PH. Ada banyak lagi.\n",
    "\n",
    "Kesimpulannya, PH tak buat apa-apa pun berkaitan kenaikan harga barang keperluan yang meresahkan rakyat semasa mereka jadi Kerajaan. Tapi sekarang tiba-tiba pula mereka jadi prihatin dan mahu jadi hero rakyat.\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "# minimum cleaning, just simply to remove newlines.\n",
    "def cleaning(string):\n",
    "    string = string.replace('\\n', ' ')\n",
    "    string = re.sub(r'[ ]+', ' ', string).strip()\n",
    "    return string\n",
    "\n",
    "# string = cleaning(string)\n",
    "string2 = cleaning(string2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1/1 [00:00<00:00, 212.73it/s]\n"
     ]
    }
   ],
   "source": [
    "input_ids, input_masks, segment_ids = get_inputs([f'ringkasan: {string2}'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "pad_sequences = tf.keras.preprocessing.sequence.pad_sequences\n",
    "\n",
    "input_ids = pad_sequences(input_ids, padding='post')\n",
    "input_masks = pad_sequences(input_masks, padding='post')\n",
    "segment_ids = pad_sequences(segment_ids, padding='post')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = sess.run(model.fast_result, feed_dict = {model.X: input_ids,\n",
    "                                        model.segment_ids: segment_ids,\n",
    "                                        model.input_masks: input_masks})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['▁',\n",
       " '-',\n",
       " '▁Harga',\n",
       " '▁barang',\n",
       " '▁mentah',\n",
       " '▁dan',\n",
       " '▁basah',\n",
       " '▁menunjukkan',\n",
       " '▁ada',\n",
       " '▁peningkatan',\n",
       " '▁di',\n",
       " '▁banyak',\n",
       " '▁tempat',\n",
       " ',',\n",
       " '▁kata',\n",
       " '▁ahli',\n",
       " '▁Majlis',\n",
       " '▁Setiausaha',\n",
       " '▁PH',\n",
       " '.',\n",
       " '▁Harga',\n",
       " '▁barang',\n",
       " '▁mentah',\n",
       " '▁dan',\n",
       " '▁basah',\n",
       " '▁menunjukkan',\n",
       " '▁ada',\n",
       " '▁peningkatan',\n",
       " '▁di',\n",
       " '▁banyak',\n",
       " '▁tempat',\n",
       " ',',\n",
       " '▁lapor',\n",
       " '▁Ap',\n",
       " '.',\n",
       " '▁Ini',\n",
       " '▁membebankan',\n",
       " '▁rakyat',\n",
       " '▁kata',\n",
       " '▁mereka',\n",
       " '.',\n",
       " '▁',\n",
       " '\"',\n",
       " 'Saya',\n",
       " '▁setuju',\n",
       " ',',\n",
       " '▁Kerajaan',\n",
       " '▁',\n",
       " 'PN',\n",
       " '▁perlu',\n",
       " '▁pantau',\n",
       " '▁dan',\n",
       " '▁mengambil',\n",
       " '▁apa',\n",
       " '-',\n",
       " 'apa',\n",
       " '▁tindakan',\n",
       " '▁yang',\n",
       " '▁perlu',\n",
       " '▁jika',\n",
       " '▁ada',\n",
       " '▁peningkatan',\n",
       " '▁harga',\n",
       " '▁barang',\n",
       " '▁basah',\n",
       " '▁atau',\n",
       " '▁mentah',\n",
       " ',',\n",
       " '\"',\n",
       " '▁kata',\n",
       " '▁seorang',\n",
       " '▁ahli',\n",
       " '▁Majlis',\n",
       " '▁Setiausaha',\n",
       " '▁PH',\n",
       " '.',\n",
       " '▁',\n",
       " '\"',\n",
       " 'Tetapi',\n",
       " '▁dalam',\n",
       " '▁masa',\n",
       " '▁yang',\n",
       " '▁sama',\n",
       " '▁saya',\n",
       " '▁fikir',\n",
       " '▁elok',\n",
       " '▁juga',\n",
       " '▁kalau',\n",
       " '▁kita',\n",
       " '▁imbas',\n",
       " '▁balik',\n",
       " '▁apa',\n",
       " '▁pula',\n",
       " '▁tindakan',\n",
       " '▁yang',\n",
       " '▁diambil',\n",
       " '▁oleh',\n",
       " '▁Kerajaan',\n",
       " '▁PH',\n",
       " '▁ketika',\n",
       " '▁rakyat',\n",
       " '▁tertekan',\n",
       " '▁dengan',\n",
       " '▁harga',\n",
       " '▁barang',\n",
       " '▁yang',\n",
       " '▁melonjak',\n",
       " '▁gila',\n",
       " '-',\n",
       " 'gil',\n",
       " 'a',\n",
       " '▁masa',\n",
       " '▁mereka',\n",
       " '▁menjadi',\n",
       " '▁Kerajaan',\n",
       " '▁Persekutuan',\n",
       " '▁selama',\n",
       " '▁22',\n",
       " '▁bulan',\n",
       " '▁dulu',\n",
       " '.',\n",
       " '▁Mari',\n",
       " '▁kita',\n",
       " '▁semak',\n",
       " '▁balik',\n",
       " '.',\n",
       " '▁',\n",
       " '(',\n",
       " '1',\n",
       " ')',\n",
       " '▁Kenaikan',\n",
       " '▁harga',\n",
       " '▁telur',\n",
       " '▁Saifuddin',\n",
       " '▁Nasution',\n",
       " '▁',\n",
       " '-',\n",
       " '▁',\n",
       " '\"',\n",
       " 'Kementerian',\n",
       " '▁akan',\n",
       " '▁siasat',\n",
       " '.',\n",
       " '▁',\n",
       " '\"',\n",
       " '▁Kenaikan',\n",
       " '▁harga',\n",
       " '▁barang',\n",
       " '▁kawalan',\n",
       " '▁Saifuddin',\n",
       " '▁Nasution',\n",
       " '▁',\n",
       " '-',\n",
       " '▁',\n",
       " '\"',\n",
       " 'KPN',\n",
       " 'H',\n",
       " 'EP',\n",
       " '▁tidak',\n",
       " '▁menerima',\n",
       " '▁sebarang',\n",
       " '▁aduan',\n",
       " '▁kenaikan',\n",
       " '▁harga',\n",
       " '▁barang',\n",
       " '▁kawalan',\n",
       " '.',\n",
       " '▁Kita',\n",
       " '▁akan',\n",
       " '▁siasat',\n",
       " '.',\n",
       " '▁',\n",
       " '\"',\n",
       " '▁Kenaikan',\n",
       " '▁harga',\n",
       " '▁barang',\n",
       " '▁kawalan',\n",
       " '▁Saifuddin',\n",
       " '▁Nasution',\n",
       " '▁',\n",
       " '-',\n",
       " '▁',\n",
       " '\"',\n",
       " 'KPN',\n",
       " 'H',\n",
       " 'EP',\n",
       " '▁tidak',\n",
       " '▁menerima',\n",
       " '▁sebarang',\n",
       " '▁aduan',\n",
       " '▁kenaikan',\n",
       " '▁harga',\n",
       " '▁barang',\n",
       " '▁kawalan',\n",
       " '.',\n",
       " '▁Kita',\n",
       " '▁akan',\n",
       " '▁siasat',\n",
       " '.',\n",
       " '▁',\n",
       " '\"',\n",
       " '▁Kenaikan',\n",
       " '▁harga',\n",
       " '▁barang',\n",
       " '▁kawalan',\n",
       " '▁Saifuddin',\n",
       " '▁Nasution',\n",
       " '▁',\n",
       " '-',\n",
       " '▁',\n",
       " '\"',\n",
       " 'KPN',\n",
       " 'H',\n",
       " 'EP',\n",
       " '▁tidak',\n",
       " '▁menerima',\n",
       " '▁sebarang',\n",
       " '▁aduan',\n",
       " '▁kenaikan',\n",
       " '▁harga',\n",
       " '▁barang',\n",
       " '▁kawalan',\n",
       " '.',\n",
       " '▁Kita',\n",
       " '▁akan',\n",
       " '▁siasat',\n",
       " '.',\n",
       " '▁',\n",
       " '\"',\n",
       " '▁Kenaikan',\n",
       " '▁harga',\n",
       " '▁barang',\n",
       " '▁kawalan',\n",
       " '▁Saifuddin',\n",
       " '▁Nasution',\n",
       " '▁',\n",
       " '-',\n",
       " '▁',\n",
       " '\"',\n",
       " 'KPN',\n",
       " 'H',\n",
       " 'EP',\n",
       " '▁tidak',\n",
       " '▁menerima',\n",
       " '▁sebarang',\n",
       " '▁aduan',\n",
       " '▁kenaikan',\n",
       " '▁harga',\n",
       " '▁barang',\n",
       " '▁kawalan',\n",
       " '.',\n",
       " '▁Kita',\n",
       " '▁akan',\n",
       " '▁siasat',\n",
       " '.',\n",
       " '▁',\n",
       " '\"',\n",
       " '▁Kenaikan',\n",
       " '▁harga',\n",
       " '▁barang',\n",
       " '▁kawalan',\n",
       " '▁Saifuddin',\n",
       " '▁Nasution',\n",
       " '▁',\n",
       " '-',\n",
       " '▁',\n",
       " '\"',\n",
       " 'KPN',\n",
       " 'H',\n",
       " 'EP',\n",
       " '▁tidak',\n",
       " '▁menerima',\n",
       " '▁sebarang',\n",
       " '▁aduan',\n",
       " '▁kenaikan',\n",
       " '▁harga',\n",
       " '▁barang',\n",
       " '▁kawalan',\n",
       " '.',\n",
       " '▁Kita',\n",
       " '▁akan',\n",
       " '▁siasat',\n",
       " '.',\n",
       " '▁',\n",
       " '\"',\n",
       " '▁Kenaikan',\n",
       " '▁harga',\n",
       " '▁barang',\n",
       " '▁kawalan',\n",
       " '▁Saifuddin',\n",
       " '▁Nasution',\n",
       " '▁',\n",
       " '-',\n",
       " '▁',\n",
       " '\"',\n",
       " 'KPN',\n",
       " 'H',\n",
       " 'EP',\n",
       " '▁tidak',\n",
       " '▁menerima',\n",
       " '▁sebarang',\n",
       " '▁aduan',\n",
       " '▁kenaikan',\n",
       " '▁harga',\n",
       " '▁barang',\n",
       " '▁kawalan',\n",
       " '.',\n",
       " '▁Kita',\n",
       " '▁akan',\n",
       " '▁siasat',\n",
       " '.',\n",
       " '▁',\n",
       " '\"',\n",
       " '▁Kenaikan',\n",
       " '▁harga',\n",
       " '▁barang',\n",
       " '▁kawalan',\n",
       " '▁Saifuddin',\n",
       " '▁Nasution',\n",
       " '▁',\n",
       " '-',\n",
       " '▁',\n",
       " '\"',\n",
       " 'KPN',\n",
       " 'H',\n",
       " 'EP',\n",
       " '▁tidak',\n",
       " '▁menerima',\n",
       " '▁sebarang',\n",
       " '▁aduan',\n",
       " '▁kenaikan',\n",
       " '▁harga',\n",
       " '▁barang',\n",
       " '▁kawalan',\n",
       " '.',\n",
       " '▁Kita',\n",
       " '▁akan',\n",
       " '▁siasat',\n",
       " '.',\n",
       " '▁',\n",
       " '\"',\n",
       " '▁Kenaikan',\n",
       " '▁harga',\n",
       " '▁barang',\n",
       " '▁kawalan',\n",
       " '▁Saifuddin',\n",
       " '▁Nasution',\n",
       " '▁',\n",
       " '-',\n",
       " '▁',\n",
       " '\"',\n",
       " 'KPN',\n",
       " 'H',\n",
       " 'EP',\n",
       " '▁tidak',\n",
       " '▁menerima',\n",
       " '▁sebarang',\n",
       " '▁aduan',\n",
       " '▁kenaikan',\n",
       " '▁harga',\n",
       " '▁barang',\n",
       " '▁kawalan',\n",
       " '.',\n",
       " '▁Kita',\n",
       " '▁akan',\n",
       " '▁siasat',\n",
       " '.',\n",
       " '</s>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>']"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tokenizer.convert_ids_to_tokens(r[0].tolist())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1/1 [00:00<00:00, 2375.03it/s]\n"
     ]
    }
   ],
   "source": [
    "input_ids, input_masks, segment_ids = get_inputs([f'soalan: siapakah Mahathir Mohamad?'])\n",
    "input_ids = pad_sequences(input_ids, padding='post')\n",
    "input_masks = pad_sequences(input_masks, padding='post')\n",
    "segment_ids = pad_sequences(segment_ids, padding='post')\n",
    "r = sess.run(model.fast_result, feed_dict = {model.X: input_ids,\n",
    "                                        model.segment_ids: segment_ids,\n",
    "                                        model.input_masks: input_masks})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['▁Mahathir', '▁Mohamad', '</s>', '<pad>', '<pad>', '<pad>']"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tokenizer.convert_ids_to_tokens(r[0].tolist())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1/1 [00:00<00:00, 2157.56it/s]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['▁Kisah',\n",
       " '▁pertama',\n",
       " '▁dari',\n",
       " '▁agama',\n",
       " '▁Kristian',\n",
       " '▁yang',\n",
       " '▁telah',\n",
       " '▁meninggal',\n",
       " '▁dunia',\n",
       " '▁pada',\n",
       " '▁abad',\n",
       " '▁ke',\n",
       " '-',\n",
       " '1',\n",
       " '▁Masihi',\n",
       " '</s>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>',\n",
       " '<pad>']"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "input_ids, input_masks, segment_ids = get_inputs([f'chatbot: ceritakan kepada saya tentang agama'])\n",
    "input_ids = pad_sequences(input_ids, padding='post')\n",
    "input_masks = pad_sequences(input_masks, padding='post')\n",
    "segment_ids = pad_sequences(segment_ids, padding='post')\n",
    "r = sess.run(model.fast_result, feed_dict = {model.X: input_ids,\n",
    "                                        model.segment_ids: segment_ids,\n",
    "                                        model.input_masks: input_masks})\n",
    "\n",
    "tokenizer.convert_ids_to_tokens(r[0].tolist())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "base/\n",
      "base/model.ckpt-175000.index\n",
      "base/model.ckpt-175000.meta\n",
      "base/model.ckpt-175000.data-00000-of-00001\n"
     ]
    }
   ],
   "source": [
    "!tar -czvf b2t.tar.gz base"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
